#include "areaform.h"
#include "ui_areaform.h"
#include <QPainter>
#include <QStyleOption>
#include <QPushButton>
#include "CommonComponents/multiplebuttonform.h"

// 静态map存储不同倍数的参数配置
// 第一级键：倍数(10,20,50)
// 第二级键：级别(1,2,3)
// 值：参数数据(4个QString)
static QMap<int, QMap<int, QVector<QString>>> m_objectiveParamsMap={
    {
        10,{{1,{"1635.98*1368.66um²", "5.35um/pixel" , "306*256pixels" , "1"}},
        {2,{"1635.98*1368.66um²", "2.67um/pixel" , "612*512pixels" , "1"}},
        {3,{"1635.98*1368.66um²", "1.34um/pixel" , "1224*1024pixels" , "1"}}
        }
    },
    {
        20,{{1,{"844.56*706.56um²", "2.76um/pixel" , "306*256pixels" , "1"}},
             {2,{"844.56*706.56um²", "1.38um/pixel" , "612*512pixels" , "1"}},
             {3,{"844.56*706.56um²", "0.69um/pixel" , "1224*1024pixels" , "1"}}
         }
    },
    {
        50,{{1,{"327.20*273.73um²", "1.07um/pixel" , "306*256pixels" , "1"}},
             {2,{"327.20*273.73um²", "0.53um/pixel" , "612*512pixels" , "1"}},
             {3,{"327.20*273.73um²", "0.27um/pixel" , "1224*1024pixels" , "1"}}
         }
    }
};


AreaForm::AreaForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AreaForm),
    m_currentMagnification(10) // 默认倍数为10倍
{
    ui->setupUi(this);
    this->setFixedHeight(190);
    this->setProperty("form","backgroundform");
    ui->leftBorderForm->SetLabelIcon("area");
    ui->widget_showInfo->ShowAll();
    
    // 设置默认选中level1
    QPushButton* level1Button = ui->widget_showInfo->findChild<QPushButton*>("pushButton_level1");
    if (level1Button) {
        level1Button->setChecked(true);
        level1Button->setEnabled(false);
    }
    
    // 初始设置为10倍一级参数
    m_currentLevel = 1;
    updateObjectiveParams(m_currentMagnification, 1);
    
    // 连接widget_showInfo的levelChanged信号到updateObjectiveParams槽函数
    connect(ui->widget_showInfo, &MultipleButtonForm::levelChanged, this, [this](int level) {
        m_currentLevel = level;
        updateObjectiveParams(m_currentMagnification, level);
    });
}

AreaForm::~AreaForm()
{
    delete ui;
}

void AreaForm::UpdateShowType(int type)
{
    ui->leftBorderForm->UpdateShowType(type);
}

void AreaForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void AreaForm::setObjectiveMagnification(int magnification)
{
    // 只在倍数变化时更新
    if (m_currentMagnification != magnification) {
        m_currentMagnification = magnification;
        

        // 更新参数显示
        updateObjectiveParams(m_currentMagnification, m_currentLevel);
    }
}

void AreaForm::updateObjectiveParams(int magnification, int level)
{
    // 检查静态map中是否存在对应的参数
    if (m_objectiveParamsMap.contains(magnification)) {
        const QMap<int, QVector<QString>>& levelParams = m_objectiveParamsMap[magnification];
        
        if (levelParams.contains(level)) {
            const QVector<QString>& params = levelParams[level];
            
            // 根据参数顺序设置各个标签的文本
            // 假设参数顺序为：面积、像素尺寸、分辨率、系数
            if (params.size() >= 4) {
                ui->label_area->setText(params[0]);       // 设置面积
                ui->label_dpiUnit->setText(params[1]);   // 设置像素尺寸
                ui->label_dpi->setText(params[2]);       // 设置分辨率
                ui->label_factor->setText(params[3]);         // 设置系数（对应label_factor）
            }
        }
    }
}

void AreaForm::on_toolButton_contractExpand_clicked(bool checked)
{
    ui->widget_hide->setHidden(!checked);
    if(checked)
        this->setFixedHeight(190);
    else
        this->setFixedHeight(50);
}
